package com.zzc.dao;

import com.zzc.bean.DiningReceipt;
import com.google.gson.Gson;
import com.zzc.bean.DiningReceipt_sum_sql_nocompany;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

public class DiningReceiptDao extends BaseDao{
	/*
select * from DiningReceipt

select dining_company,convert(varchar(10),dining_time,21) as tdate,dining_type,sum(cast(employee_number as int)) as employee_number,sum(cast(leadership_number as int)) as ddd
 from DiningReceipt
 group by dining_company,convert(varchar(10),dining_time,21),dining_type



select convert(varchar(10),dining_time,21) as tdate,dining_type,sum(cast(employee_number as int)) as employee_number,sum(cast(leadership_number as int)) as ddd
 from DiningReceipt
 group by convert(varchar(10),dining_time,21),dining_type
 order by convert(varchar(10),dining_time,21),dining_type
	 */
	public List<?> getAll(String string_timeStamp_filter){
		String hql = "select  * from DiningReceipt ";
		if (string_timeStamp_filter!=null){
			Long long_timeStamp_filter=Long.parseLong(string_timeStamp_filter);
			Date date_filter=new Date(long_timeStamp_filter);
			java.sql.Date sqlDate=new java.sql.Date(date_filter.getTime());
			hql=hql+"where 1=1 and dining_time>="+"CONVERT(varchar(10),'"+ sqlDate.toLocalDate()+"',21) ";
		}
		hql=hql+"order by dining_time DESC,dining_type,dining_id desc ";
        Query query = getSession().createSQLQuery(hql).setResultTransformer(Transformers.aliasToBean(DiningReceipt.class));
        List list = query.list();
		return list;
	}
	public List<DiningReceipt> get_employee_id(){
		String hql = "FROM DiningReceipt diningReceipt WHERE diningReceipt.employee_id=?";//hql = "FROM DiningReceipt";
		List<DiningReceipt> list_diningReceipts =getSession().createQuery(hql).list();
		String string=new Gson().toJson(list_diningReceipts);
		System.out.println(string);
		return list_diningReceipts;
	}
	public List<?> get_employee_account(String employee_account){
		String hql = "FROM DiningReceipt diningReceipt WHERE diningReceipt.employee_account=? order by dining_time DESC,dining_type";//hql = "FROM DiningReceipt";
		Query query = getSession().createQuery(hql).setString(0, employee_account);
		List<DiningReceipt> list_diningReceipts =query.list();
		return list_diningReceipts;
	}
	public DiningReceipt getSC_id(String id) {
		String hql = "FROM DiningReceipt sc WHERE sc.id=?";
		Query query = getSession().createQuery(hql).setString(0, id);
		DiningReceipt diningReceipt = (DiningReceipt) query.uniqueResult();
		//System.out.println(employee.getDepartment().getClass().getName());
		String string=new Gson().toJson(diningReceipt);
		System.out.println(string);
		return diningReceipt;
	}
	public List<DiningReceipt> getSCs_studentName(String studentName) {
		String hql = "FROM DiningReceipt sc WHERE sc.studentName=?";
		Query query = getSession().createQuery(hql).setString(0, studentName);
		List<DiningReceipt> list_diningReceipts =  query.list();
		//System.out.println(employee.getDepartment().getClass().getName());
		String string=new Gson().toJson(list_diningReceipts);
		System.out.println(string);
		return list_diningReceipts;
	}
	public List<DiningReceipt> getSCs_courseName(String courseName) {
		String hql = "FROM DiningReceipt sc WHERE sc.courseName=?";
		Query query = getSession().createQuery(hql).setString(0, courseName);
		List<DiningReceipt> list_diningReceipts =  query.list();
		//System.out.println(employee.getDepartment().getClass().getName());
		String string=new Gson().toJson(list_diningReceipts);
		System.out.println(string);
		return list_diningReceipts;
	}

	public DiningReceipt get_id(int diningReceipt_id) {
		String hql = "FROM DiningReceipt diningReceipt WHERE diningReceipt.dining_id = ?";
		Query query = getSession().createQuery(hql).setInteger(0, diningReceipt_id);
		DiningReceipt diningReceipt = (DiningReceipt) query.uniqueResult();
		return diningReceipt;
	}
}
